# ATM转账
from PyQt5 import QtWidgets, QtGui
from mysql import select_sql, insert_sql, updata_sql
import temp


class user_atm(QtWidgets.QMainWindow):
    def __init__(self):
        super(user_atm, self).__init__()
        self.card = temp.card

    def setupUI(self):
        self.setWindowTitle("卡号：" + self.card + "ATM转账")
        self.setGeometry(100, 100, 300, 200)
        self.Qlable1()
        self.Qlineedit1()
        self.Qlineedit2()
        self.Qbutton1()
        self.Qlable2()
        self.Qbutton2()

    def Qlable1(self):
        qlable = QtWidgets.QLabel(self)
        qlable.setText("请输入转入卡卡号：")
        qlable.setGeometry(10, 30, 100, 30)

    def Qlineedit1(self):
        self.qlineedit1 = QtWidgets.QLineEdit(self)
        self.qlineedit1.setGeometry(120, 30, 140, 30)
        self.qlineedit1.setValidator(QtGui.QIntValidator(self))
        self.qlineedit1.setMaxLength(10)

    def Qlable2(self):
        qlable = QtWidgets.QLabel(self)
        qlable.setText("请输入转账金额:")
        qlable.setGeometry(10, 80, 120, 30)

    def Qlineedit2(self):
        self.qlineedit2 = QtWidgets.QLineEdit(self)
        self.qlineedit2.setGeometry(120, 80, 140, 30)
        self.qlineedit2.setValidator(QtGui.QIntValidator(self))

    def Qbutton1(self):
        qbutton = QtWidgets.QPushButton(self)
        qbutton.setText("转账")
        qbutton.setGeometry(30, 140, 50, 30)
        qbutton.clicked.connect(self.Qbutton1_click)

    # 输入内容的验证
    # 先验证卡号是否为16位
    def clean_card(self):
        self.card1 = self.qlineedit1.text()
        if len(self.card1) < 10 or self.card1 == "":
            return 0
        return 1

    # 然后验证卡号是否存在
    def clean_card2(self):
        sql = "SELECT * FROM ATM.User where card = '{0}'".format(self.card1)
        result = select_sql(sql)
        if result == None:
            return 0
        return 1

    # 验证输入的内容是大于余额或者是负数
    def clean_size(self):
        self.money = int(self.qlineedit2.text())
        sql = "SELECT * FROM ATM.User where card = '{0}' ".format(self.card)
        result = select_sql(sql)
        self.money2 = result['money']
        if self.money <= 0 or self.money > self.money2 or self.money == "":
            return 0
        return 1

    # 转账
    def Qbutton1_click(self):
        clean1 = self.clean_card()
        if clean1 == 0:
            self.msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, "警告", "输入的转账卡号位数不足")
            return self.msg.show()
        else:
            clean2 = self.clean_card2()
            if clean2 == 0:
                self.msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, "警告",
                                                 "输入的卡号不存在")
                return self.msg.show()
            else:
                clean3 = self.clean_size()
                if clean3 == 0 :
                    self.msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, "警告",
                                                     "输入的金额小于0或者大于卡内余额")
                    return self.msg.show()
                else:
                    # 转账
                    # 记录
                    from datetime import datetime
                    cdate = datetime.today()
                    sql = "INSERT INTO ATM.recode(card,cdate,money,status,card2) VALUES ('{0}'," \
                          "'{1}','{2}','转账','{3}')".format(self.card, cdate, self.money,self.card1)
                    insert_sql(sql)
                    # 更新自己
                    money = self.money2 - self.money
                    sql = "UPDATE ATM.User SET ATM.User.money = '{0}' where card = '{1}'".format(money,self.card)
                    updata_sql(sql)
                    # 更新对方
                    sql = "SELECT * FROM ATM.User where card = '{0}' ".format(self.card1)
                    result = select_sql(sql)
                    money2 = result['money']
                    money3 = money2 + self.money
                    sql = "UPDATE ATM.User SET ATM.User.money = '{0}' where card = '{1}'".format(money3, self.card1)
                    updata_sql(sql)
                    # 成功后回到主窗口
                    self.msg = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Information, "提示",
                                                     "转账成功")
                    return self.msg.show()

    def Qbutton2(self):
        qbutton = QtWidgets.QPushButton(self)
        qbutton.setText("返回")
        qbutton.setGeometry(100, 140, 50, 30)
        qbutton.clicked.connect(self.Qbutton2_click)

    def Qbutton2_click(self):
        self.close()
        from user_main import user_main
        self.a = user_main()
        self.a.setupUI()
        self.a.show()
